package com.zhangfenghua.shoppingMall;

import java.util.Scanner;
//购物车案例：具有增加商品，查询商品，修改商品数量，结算商品价格四个功能
public class ShoppingCart {
    public static void main(String[] args) {
        //定义一个用来存放商品的数组，类是引用数据类型
        Goods[] shoppingCart=new Goods[100];
        while (true) {
            System.out.println("增加商品到购物车请按：add");
            System.out.println("查询购物车中的商品请按：query");
            System.out.println("修改购物车中商品数量请按：update");
            System.out.println("结算购物车里商品金额请按：pay");
            Scanner sc = new Scanner(System.in);
            System.out.print("请选择功能：");
            String command = sc.next();
            switch (command) {
                case "add":
                    //添加方法
                    addGoods(shoppingCart,sc);
                    break;
                case "query":
                    //查询方法
                    queryGoods(shoppingCart);
                    break;
                case "update":
                    //修改方法
                    updateGoods(shoppingCart,sc);
                    break;
                case "pay":
                    //结算方法
                    payGoods(shoppingCart);
                    break;
                default:
                    System.out.println("功能尚未开发中，敬请期待！！！");
            }
        }
    }

    public static void addGoods(Goods[] shoppingCart,Scanner sc) {
        System.out.println("请输入商品编号(不重复)：");
        int id = sc.nextInt();
        System.out.println("请输入商品名称：");
        String name = sc.next();
        System.out.println("请输入商品价格：");
        double price = sc.nextDouble();
        System.out.println("请输入商品数量：");
        int amount = sc.nextInt();
        //把添加商品的信息封装成一个商品对象
        Goods goods = new Goods();
        goods.id=id;
        goods.name=name;
        goods.price=price;
        goods.amount=amount;
        for (int i = 0; i < shoppingCart.length; i++) {
            if (shoppingCart[i]==null){
                shoppingCart[i]=goods;
                break;
            }
        }
        System.out.println(goods.name+"已成功添加到购物车！");
    }
    public static void queryGoods(Goods[] shoppingCart) {
        //查询购物车中商品信息
        System.out.println("==========购物车中商品信息如下==========");
        System.out.println("商品编号"+"\t\t\t"+"商品名称"+"\t\t\t"+"商品价格"+"\t\t\t"+"商品数量");
        for (int i = 0; i < shoppingCart.length; i++) {
            Goods goods = shoppingCart[i];
            if (goods!=null){
                System.out.println(goods.id+"\t\t\t"+goods.name+"\t\t\t"+goods.price+"\t\t\t"+goods.amount);
                System.out.println("==========购物车中商品信息如上==========");
            }else{
                //遍历结束
                break;
            }
        }
    }
    public static void updateGoods(Goods[] shoppingCart,Scanner sc) {
        //根据商品id查看商品是否存在与购物车中
        while (true) {
            System.out.println("请输入要修改商品的id：");
            int id = sc.nextInt();
            Goods g = queryid(shoppingCart, id);
            if (g==null){
                System.out.println("对不起没有购买该商品");
            }else{
                System.out.println("请输入"+g.name+"的最新数量：");
                int i=sc.nextInt();
                g.amount=i;
                System.out.println("修改成功");
                queryGoods(shoppingCart);
                break;
            }
        }

    }
    //根据商品id查询商品
    public static Goods queryid(Goods[] shoppingCart,int id){
        for (int i = 0; i < shoppingCart.length; i++) {
            Goods goods = shoppingCart[i];
            //查看数组是否为空
            if (goods!=null){
                //判断输入的id是否存在与goods数组
                if (goods.id==id){
                    return goods;
                }
            }else{
                return null;
            }
        }
        return null;
    }
    public static void payGoods(Goods[] shoppingCart) {
        queryGoods(shoppingCart);
        //定义一个累加金额的变量
        double money=0;
        for (int i = 0; i < shoppingCart.length; i++) {
            Goods goods = shoppingCart[i];
            if (goods!=null){
                money+=(goods.price*goods.amount);
            }else{
                break;
            }
        }
        System.out.println("清空购物车所需金额为："+money);
    }
}
